package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Tipoevento;
import services.TipoEventoService;

@FacesConverter("tipoeventoConverter")
public class TipoEventoConverter implements Converter {

	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            TipoEventoService service = (TipoEventoService) fc.getExternalContext().getApplicationMap().get("tipoeventoService");
            Iterator<Tipoevento> i=service.getListTipoEvento().iterator();
            while (i.hasNext()) {
            	Tipoevento t = (Tipoevento) i.next();
				if(t.getIdTipoEvento() == Integer.parseInt(value))
					return t;
			}
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
        	return String.valueOf(((Tipoevento) object).getIdTipoEvento());
        }
        else {
            return null;
        }
    }  
	
}
